package com.gta.edu.sdk.base.controller;

import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

import com.gta.edu.sdk.base.call.IUserAuthServerCall;
import com.gta.edu.sdk.common.dto.LoginUserInfo;
import com.gta.edu.sdk.log.contant.LogType;
import com.gta.edu.sdk.log.entity.TSysLogInfo;
import com.gta.edu.sdk.util.RedisClusterUtil;
import com.gta.edu.sdk.util.StringUtil;

import net.sf.json.JSONObject;
@RestController
public class BaseController {
	private static final Logger log = LogManager.getLogger(BaseController.class);
	
	private String tokenParamName = "token";
	@Value("${sys-code}")
	private String sysCode;
	
	@Autowired  
	private HttpServletRequest request;
	private final String LOG_REDIS_KEY = "OPERATE_LOG_KEY";
	
	@Autowired  
	private IUserAuthServerCall userAuthServerCall;
	
	
	/**
	 * @description 得到用户登陆的ticket数据
	 * @author fengya
	 * @date 2017年5月26日 上午9:05:45
	 * @return
	 * @throws 
	 * @return String
	 */
	public String getTicket(){
		Cookie cookies[] = request.getCookies();
//		String ticket = WebUtils.getCookie((HttpServletRequest) request,"ticket").getValue();
		String ticket = null;
		if(cookies != null){
			for(Cookie ck: cookies){
				if(ck.getName().trim().equals(tokenParamName)){
					ticket = ck.getValue();
					break;
				}
			}
		}
		return ticket;
	}
	
	/**
	 * @description 写用户操作日志
	 * @author fengya
	 * @date 2020年12月14日 下午3:01:23
	 * @param log
	 * void
	 */
	public void writeOperationLog(TSysLogInfo log) {
		LoginUserInfo userInfo = this.getLongUserInfo();
		if (request.getHeader("x-forwarded-for") == null) {
			log.setIp(request.getRemoteAddr());
		} else {
			log.setIp(request.getHeader("x-forwarded-for"));
		}
//		log.setIp(request.getRemoteAddr());
		log.setLogType(LogType.BUSINESS.getValue());
		log.setOper(userInfo.getIscUserSourceId());
		log.setOperName(userInfo.getIscAdCode());
		log.setOpTime(new Date());
		log.setSysCode(sysCode);
		RedisClusterUtil.getInstance().addList(LOG_REDIS_KEY, log);
	}
	
	/**
	 * @description 得到登陆用户的用户信息
	 * @author fengya
	 * @date 2017年5月26日 上午9:08:55
	 * @return
	 * @throws 
	 * @return LoginUserInfo
	 */
	public LoginUserInfo getLongUserInfo() {
		String ticket = this.getTicket();
		if (StringUtil.isNullOrEmpty(ticket)) {
			log.error("获取登录用户的ticket信息为空！请检查ticket对应的token配置信息是否正确，获取从浏览器的cookie中查看是否存在token数据");
			return null;
		}
		String loginUserResultStr = this.userAuthServerCall.getLoginUser(ticket);
		LoginUserInfo loginUserInfo = null;
		if (StringUtil.isNullOrEmpty(loginUserResultStr)) {
			log.error("获取登录用户信息为空，当前为未调用用户认证接口，而且也未调用回调函数！");
		} else {
			JSONObject loginUserResultJson = JSONObject.fromObject(loginUserResultStr);
			if (loginUserResultJson != null) {
				if (loginUserResultJson.getBoolean("success")) {
					String loginUserStr = loginUserResultJson.getString("data");
					log.info(loginUserStr);
					loginUserInfo = this.madeLoginUserInfo(loginUserStr);
				} else {
					log.error(loginUserResultJson.getString("errorMsg"));
				}
			} else {
				log.error("获取登录用户信息错误，无法转变为对象信息！");
			}
		}
		return loginUserInfo;
	}
	
	/**
	 * @description 根据登录用户的字符串信息构造成用户对象
	 * @author fengya
	 * @date 2020年4月7日 上午9:08:55
	 * @param loginUserStr 登录用户的json字符串信息
	 * @return LoginUserInfo 登录用户的对象信息
	 */
	private LoginUserInfo madeLoginUserInfo(String loginUserStr) {
		LoginUserInfo loginUserInfo = null;
		if (!StringUtil.isNullOrEmpty(loginUserStr)) {
			JSONObject loginUserJson = JSONObject.fromObject(loginUserStr);
			loginUserInfo = (LoginUserInfo)JSONObject.toBean(loginUserJson, LoginUserInfo.class);
		}
		return loginUserInfo;
	}
}
